//
//  AuditPersonViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/5/25.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "AuditPersonViewC.h"
#import "UserModel.h"
#import "WarEntity.h"
#import <UIImageView+AFNetworking.h>

@interface AuditCell : UITableViewCell{
    
}
@property (weak, nonatomic) IBOutlet UIImageView *headerImg;
@property (weak, nonatomic) IBOutlet UILabel *nickLab;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UILabel *phontLab;
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
@property (weak, nonatomic) IBOutlet UIButton *disagrBtn;
@property (weak, nonatomic) IBOutlet UIButton *passBtn;
@end

@implementation  AuditCell
- (void)viewDidLoad{
    [self viewDidLoad];
    self.passBtn.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [self viewWillDisappear:YES];
    self.headerImg.layer.cornerRadius = 30;
    self.headerImg.clipsToBounds = YES;
}
@end

@interface AuditPersonViewC (){
    NSArray *dataArray;
}

@end

@implementation AuditPersonViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"审核约战人员";
    [self getData];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getData{
    
    NSString *num = self.warID;
    [[UserModel sharedInstance] mywarPersonWithwid:num myData:^(id info) {
        NSArray *array = [info copy];
        dataArray = array;
        [self.tableView reloadData];
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // Return the number of rows in the section.
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75.f;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AuditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuditCellID"
                             forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WarEntity *warEty = dataArray[indexPath.row];
    cell.nickLab.text = warEty.wnick;
    cell.addressLab.text = warEty.address;
    cell.phontLab.text = [NSString stringWithFormat:@"电话:%@",
                          warEty.userPhone];
    NSString *headimg = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",
                         warEty.wheadpic];
    NSURL *headurl = [NSURL URLWithString:headimg];
    [cell.headerImg setImageWithURL:headurl];
    [cell.agreeBtn addTarget:self action:@selector(agreeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.disagrBtn addTarget:self action:@selector(disagrBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    NSString *status = [NSString stringWithFormat:@"%@",warEty.isSelect];
    if ([status isEqualToString:@"1"]) {
        cell.agreeBtn.hidden =YES;
        cell.disagrBtn.hidden = YES;
        cell.passBtn.hidden = NO;
        cell.passBtn.enabled = NO;
    }
    
    
    return cell;
}

-(void)agreeBtnAction:(id) sender{
    UIButton *btn=(UIButton *)sender;
    WarEntity *warEty = [dataArray objectAtIndex:btn.tag];
    NSString *warid = [NSString stringWithFormat:@"%@",warEty.yuezhanId];
    NSString *applyid = [NSString stringWithFormat:@"%@",
                          warEty.yuezhanApplyId];
    NSString *status =@"1";
    [[UserModel sharedInstance] setApplrWithwarID:warid applyid:applyid status:status getStatus:^(BOOL successed, unsigned int value) {
        if (successed) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                                  @"提示" message:@"通过申请失败"
                                                           delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];

    

}

-(void)disagrBtnAction:(id) sender{
    UIButton *btn=(UIButton *)sender;
    WarEntity *warEty = [dataArray objectAtIndex:btn.tag];
    NSString *warid = [NSString stringWithFormat:@"%@",warEty.yuezhanId];
    NSString *applyid = [NSString stringWithFormat:@"%@",
                         warEty.yuezhanApplyId];
    NSString *status =@"2";
    [[UserModel sharedInstance] setApplrWithwarID:warid applyid:applyid status:status getStatus:^(BOOL successed, unsigned int value) {
        if (successed) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                                  @"提示" message:@"取消申请失败"
                                                           delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
